//
//  YMSStatusCell.swift
//  YMSWeibo
//
//  Created by 杨茂盛 on 2016/11/16.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
import SDWebImage
private let margin: CGFloat = 8
//配图视图之间的间距
private let imageMargin: CGFloat = 2
private let maxWidth = ScreenWidth - 2 * margin
private let itemWidth = (maxWidth - 2 * imageMargin) / 3

class YMSStatusCell: UITableViewCell {
    //用户头像
    @IBOutlet weak var iconView: UIImageView!
    //用户名字
    @IBOutlet weak var userName: UILabel!
    //用户等级
    @IBOutlet weak var userLevel: UIImageView!
    //微博时间
    @IBOutlet weak var time: UILabel!
    //来源
    @IBOutlet weak var source: UILabel!
    //微博正文
    @IBOutlet weak var content: YYLabel!
    //用户认证类型
    @IBOutlet weak var avatarView: UIImageView!
  //配图视图
    @IBOutlet weak var pictureView: YMSPictureView!
    @IBOutlet weak var pictureWidthConst: NSLayoutConstraint!
    @IBOutlet weak var pictureHeightConst: NSLayoutConstraint!
    @IBOutlet weak var pictureViewFlowLayout: UICollectionViewFlowLayout!
    //底部工具条
    @IBOutlet weak var toolbar: UIView!
    
    @IBOutlet weak var retweenLabel: UILabel!
//    @IBOutlet weak var pictureViewTop: NSLayoutConstraint!
    
   
    var viewModel :YMSStatusesViewModel?{
        didSet{
            //设置子控件
            iconView.sd_setImage(with: viewModel?.iconUrl)
            userName.text = viewModel?.status?.user?.name
            time.text = viewModel?.timeText
            source.text = viewModel?.sourceText
            content.attributedText = viewModel?.originalAttributedString //使用富文本
            avatarView.image = viewModel?.verifitedImage
            userLevel.image = viewModel?.mbRankImage
            
            let count = viewModel?.imageUrls?.count ?? 0
            //pictureViewTop.constant = (count == 0 ? 0 : margin)
            //let pSize = caculatePicSize(count: count)
            
            let result = caculatePicSize(count: count)
            //根据约束对象的约束值
            pictureWidthConst.constant = result.pSize.width
            pictureHeightConst.constant = result.pSize.height
            
            //还要更新itemSize 会出现复用的问题
            pictureViewFlowLayout.itemSize = result.itemSize
            
            //给配图视图设置数据源   //给转发微博的配图视图设置数据源
            //pictureView.pictureInfos = viewModel?.status?.pic_urls
            pictureView.pictureInfos = viewModel?.imageUrls
            retweenLabel?.text = viewModel?.status?.retweeted_status?.text
            
            //需要重新布局
            pictureView.layoutIfNeeded()
            
            //实现点击高亮的闭包
            content.highlightTapAction = { (containerView,text,range,rect) -> () in
                //containerView,就是被点击的视图
                //text: 被点击的属性文本
                //range: 点击的特殊文本 范围
                //rect: 点击的特殊的文本的坐标
                var sub = (text.string as NSString).substring(with: range)
               // print(sub)
                //跳转页面   响应者链条来实现页面跳转
                //当用户点击url的时候,跳转界面 字符如果是http开头,就进行跳转
                if sub.hasPrefix("http"){
                
                let vc = YMSDtailController()
                    vc.urlString = sub
                //寻找vc的navigationVc 并进行跳转
                    self.findNavController()?.pushViewController(vc, animated: true)
                }
                
            }
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置cell的选中类型
        self.selectionStyle = .none
        content.numberOfLines = 0
        self.content.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
        //转发微博的内容
        self.retweenLabel?.numberOfLines = 0
        self.retweenLabel?.preferredMaxLayoutWidth = ScreenWidth - 2 * margin

        //设置流水布局
        pictureViewFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //设置行和列最小间距
        pictureViewFlowLayout.minimumLineSpacing = imageMargin
        pictureViewFlowLayout.minimumInteritemSpacing = imageMargin
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    //根据图片的张数计算配图视图的大小,更新配图视图的宽高对象的值
    //元组类型: 可以使用多个类型合成一个类型
    private func caculatePicSize(count:Int) -> (pSize: CGSize,itemSize: CGSize){
        if count == 0{
        return (CGSize(width: 1, height: 1),CGSize(width: 1, height: 1))
        }
        //判断一张图
        if count == 1 {
            //计算图片的大小 imageSize 必须保证这里一定能够获取到图片
            //不能够在这里下载图片,否则有多次刷新(有多少张图片就要刷新是多少次)
            
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.imageUrls?.last?.wap360 ?? ""){
                //计算图片的大小
                let imageSize = image.size
                //不能在if中单独设置itemSize
                return (imageSize,imageSize)
            }
            //返回itemSize
            return (CGSize(width: itemWidth, height: itemWidth),CGSize(width: itemWidth, height: itemWidth))
        }
        
        //判断四张图
        if count == 4 {
            let width = 2*itemWidth + margin
            return (CGSize(width: width, height: width),CGSize(width: itemWidth, height: itemWidth))
        }
        //其他多余的多张图片 需要确定行数
        
        /*
         1,2,3  1
         4,5,6 2
         7,8,9 3
         */
        
        //已知列数和总个数 需要确定行数
        let row = CGFloat((count - 1) / 3) + 1
        let h = row * itemWidth + (row - 1)*imageMargin
        return (CGSize(width: maxWidth, height: h),CGSize(width: itemWidth, height: itemWidth))
    }
}
